package com.ljy.my_study.callable;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableDemo {
	public static void main(String[] args) {
		ExecutorService executor=Executors.newSingleThreadExecutor();
		CallableThread[] tasks=new CallableThread[10];
		for(int i=0;i<tasks.length;i++) {
			tasks[i]=new CallableThread();
		}
		Future<Integer>[] futures=new Future[10];
		for(int i=0;i<tasks.length;i++) {
			futures[i]=executor.submit(tasks[i]);
		}
		System.out.println("主线程开始工作。。。");
		try {
			Thread.sleep(2000);
			Integer sum=0;
			for(int i=0;i<futures.length;i++) {
				try {
					System.out.println("result: "+futures[i].get());
					int futureResult=futures[i].get();
					sum+=futureResult;
					System.out.println("result sum:"+sum);
				} catch (ExecutionException e) {
					System.out.println("get result failed");
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("主线程结束工作。。。");
		executor.shutdown();
	}
}
